import React, { Component } from 'react';
import { NavLink,withRouter} from 'react-router-dom';
import RouterView from '../../router';

class My extends Component {
    state={
        name:sessionStorage.getItem('token'),
    }
    render() {  
        let {name}=this.state  
        return (
            <div>
                <h1>{name?name+' 你好':'游客'}</h1>
                <RouterView routes={this.props.routes}></RouterView>
                <ul>
                    <li><NavLink to='/layout/my/part1' children='p1'></NavLink></li>
                    <li><NavLink to='/layout/my/part2' children='p2'></NavLink></li>
                </ul>
                {name?<button onClick={this.logout}>退出登录</button>:<button onClick={this.goLogin}>去登录</button>}          
            </div>
        );
    }
    goLogin=()=>{
        this.props.history.push('/login')
    }
    logout=()=>{
        sessionStorage.removeItem('token')
        this.setState({name:null})
    }
}

export default withRouter(My);